package com.xthh.sql;


import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


import com.xthh._enum.HH_EnumBindDeviceINI;
import com.xthh._enum.HH_EnumDeviceColorType;
import com.xthh._enum.HH_EnumOnLineState;
import com.xthh._function.HH_CharUtil;
import com.xthh._function.HH_TimeUtil;
import com.xthh._struct.HH_T_BindDevice;

import java.util.ArrayList;
import java.util.List;

/**
 * api查询方式
 */
public class HH_BindDeviceDao2 {

    private static final String TAG = "BindDeviceDao2";
    HH_XTSQLiteOpenHelper mOpenHelper;//数据库的帮助类

    public HH_BindDeviceDao2(Context context, Application application) {
        mOpenHelper = new HH_XTSQLiteOpenHelper(context, application);
    }


    /**
     * 添加到BindDevice表一条数据
     */
    public void insert(HH_T_BindDevice tr, boolean writeNAME) {
        HH_T_BindDevice trFind = queryItem(tr.getMacRes());
        if (trFind == null) {
            SQLiteDatabase db = mOpenHelper.getWritableDatabase();
            if (db.isOpen()) {  //如果数据库打开
                ContentValues values = new ContentValues();
                values.put(HH_EnumBindDeviceINI.proType.toString(), HH_CharUtil.Itoa(tr.getProType()));
                values.put(HH_EnumBindDeviceINI.name.toString(), tr.getName());
                values.put(HH_EnumBindDeviceINI.macRes.toString(), tr.getMacRes());
                values.put(HH_EnumBindDeviceINI.connectState.toString(), HH_CharUtil.BToS(tr.isConnectState()));
                values.put(HH_EnumBindDeviceINI.dbm.toString(), HH_CharUtil.Itoa(tr.getDbm()));
                values.put(HH_EnumBindDeviceINI.sel.toString(), HH_CharUtil.BToS(tr.isSelect()));
                values.put(HH_EnumBindDeviceINI.picPath_A.toString(), tr.getPicPath_A());
                values.put(HH_EnumBindDeviceINI.picPath_B.toString(), tr.getPicPath_B());
                values.put(HH_EnumBindDeviceINI.bwReversal.toString(), HH_CharUtil.BToS(tr.isBwReversal()));
                values.put(HH_EnumBindDeviceINI.bz.toString(), tr.getBz());
                values.put(HH_EnumBindDeviceINI.deviceColorType.toString(), HH_CharUtil.Itoa(tr.getDeviceColorType().ordinal()));
                values.put(HH_EnumBindDeviceINI.picIndex.toString(), HH_CharUtil.Itoa(tr.getPicIndex()));
                values.put(HH_EnumBindDeviceINI.picAttr.toString(), HH_CharUtil.Itoa(tr.getPicAttr()));
                values.put(HH_EnumBindDeviceINI.ver.toString(), tr.getVer());
                values.put(HH_EnumBindDeviceINI.width.toString(), HH_CharUtil.Itoa(tr.getWidth()));
                values.put(HH_EnumBindDeviceINI.height.toString(), HH_CharUtil.Itoa(tr.getHeight()));
                values.put(HH_EnumBindDeviceINI.size.toString(), tr.getSize());
                values.put(HH_EnumBindDeviceINI.batteryB.toString(), HH_CharUtil.Itoa(tr.getBatteryB()));
                values.put(HH_EnumBindDeviceINI.onlineState.toString(), HH_CharUtil.Itoa(tr.getOnlineState().ordinal()));
                values.put(HH_EnumBindDeviceINI.isWithNewspush.toString(), HH_CharUtil.Itoa(tr.getIsWithNewspush()));
                values.put(HH_EnumBindDeviceINI.isWithFrontLight.toString(), HH_CharUtil.Itoa(tr.getIsWithFrontLight()));
                values.put(HH_EnumBindDeviceINI.isWithLED.toString(), HH_CharUtil.Itoa(tr.getIsWithLED()));
                values.put(HH_EnumBindDeviceINI.rotate_A.toString(), HH_CharUtil.Itoa(tr.getRotate_A()));
                values.put(HH_EnumBindDeviceINI.rotate_B.toString(), HH_CharUtil.Itoa(tr.getRotate_B()));
                values.put(HH_EnumBindDeviceINI.chipType.toString(), HH_CharUtil.Itoa(tr.getChipType()));
                values.put(HH_EnumBindDeviceINI.sendPower.toString(), HH_CharUtil.Itoa(tr.getSendPower()));
                values.put(HH_EnumBindDeviceINI.hardwareVer.toString(), tr.getHardwareVer());
                values.put(HH_EnumBindDeviceINI.deviceName.toString(), tr.getDeviceName());
                values.put(HH_EnumBindDeviceINI.contraryROW.toString(), HH_CharUtil.Itoa(tr.getContraryROW()));
                values.put(HH_EnumBindDeviceINI.contraryCOL.toString(), HH_CharUtil.Itoa(tr.getContraryCOL()));
                values.put(HH_EnumBindDeviceINI.keyCount.toString(), HH_CharUtil.Itoa(tr.getKeyCount()));
                values.put(HH_EnumBindDeviceINI.deviceNumber.toString(), HH_CharUtil.Itoa(tr.getDeviceNumber()));
                values.put(HH_EnumBindDeviceINI.deviceType.toString(), HH_CharUtil.Itoa(tr.getDeviceType()));

                long id = db.insert(HH_SheetName.SQL_SHEET_NAME_BIND_DEVICE, null, values);

                Log.i(TAG, id + "");

                db.close(); //数据库关闭
            }
        }
    }

    public void deleteAll() {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();//获得可写入数据库对象
        if (db.isOpen()) {
            db.execSQL("delete from " + HH_SheetName.SQL_SHEET_NAME_BIND_DEVICE);
            db.close();
        }
    }

    public void delete(String mac) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();//获得可写入数据库对象
        if (db.isOpen()) {
            String whereClasuse = HH_EnumBindDeviceINI.macRes.toString() + " = ?";
            String[] whereArgs = {mac};
            int count = db.delete(HH_SheetName.SQL_SHEET_NAME_BIND_DEVICE, whereClasuse, whereArgs);

            Log.i(TAG, "删除了:" + count);
            db.close();
        }
    }

    public void update_name(String mac, String name) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        if (db.isOpen()) {
            ContentValues values = new ContentValues();
            values.put(HH_EnumBindDeviceINI.name.toString(), name);
            String whereClause = HH_EnumBindDeviceINI.macRes.toString() + " = ?";
            int count = db.update(HH_SheetName.SQL_SHEET_NAME_BIND_DEVICE, values, whereClause, new String[]{mac});
            db.close();
        }
    }


    public List<HH_T_BindDevice> queryAll() {
        List<HH_T_BindDevice> personList = new ArrayList<HH_T_BindDevice>();

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        if (db.isOpen()) {

            String[] columns = new String[HH_EnumBindDeviceINI.values().length];
            for (HH_EnumBindDeviceINI e : HH_EnumBindDeviceINI.values()) {
                columns[e.ordinal()] = e.name();
            }

            String sCurrentTime = HH_TimeUtil.GetCurrentSystemTimeF();

            String selection = null; // 选择条件，给null查询所有
            String[] selectionArgs = null;//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null; //排序

            Cursor cursor = db.query(HH_SheetName.SQL_SHEET_NAME_BIND_DEVICE, columns, selection, selectionArgs, groupBy, having, orderBy);

            if (cursor != null && cursor.getCount() > 0) {

                while (cursor.moveToNext()) { //向下移动一位，直到最后一位,不可以往下移动了，停止

                    HH_T_BindDevice tr = new HH_T_BindDevice();
                    tr.setProType(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.proType.ordinal())));
                    tr.setName(cursor.getString(HH_EnumBindDeviceINI.name.ordinal()));
                    tr.setMacRes(cursor.getString(HH_EnumBindDeviceINI.macRes.ordinal()));
                    tr.setConnectState(HH_CharUtil.SToB(cursor.getString(HH_EnumBindDeviceINI.connectState.ordinal())));
                    tr.setDbm(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.dbm.ordinal())));
                    tr.setSelect(HH_CharUtil.SToB(cursor.getString(HH_EnumBindDeviceINI.sel.ordinal())));
                    tr.setPicPath_A(cursor.getString(HH_EnumBindDeviceINI.picPath_A.ordinal()));
                    tr.setPicPath_B(cursor.getString(HH_EnumBindDeviceINI.picPath_B.ordinal()));
                    tr.setBwReversal(HH_CharUtil.SToB(cursor.getString(HH_EnumBindDeviceINI.bwReversal.ordinal())));
                    tr.setBz(cursor.getString(HH_EnumBindDeviceINI.bz.ordinal()));
                    tr.setDeviceColorType(HH_EnumDeviceColorType.values()[HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.deviceColorType.ordinal()))]);
                    tr.setPicIndex(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.picIndex.ordinal())));
                    tr.setPicAttr(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.picAttr.ordinal())));
                    tr.setVer(cursor.getString(HH_EnumBindDeviceINI.ver.ordinal()));
                    tr.setWidth(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.width.ordinal())));
                    tr.setHeight(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.height.ordinal())));
                    tr.setSize(cursor.getString(HH_EnumBindDeviceINI.size.ordinal()));
                    tr.setBatteryB(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.batteryB.ordinal())));
                    tr.setOnlineState(HH_EnumOnLineState.values()[HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.onlineState.ordinal()))]);
                    tr.setIsWithNewspush(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.isWithNewspush.ordinal())));
                    tr.setIsWithFrontLight(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.isWithFrontLight.ordinal())));
                    tr.setIsWithLED(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.isWithLED.ordinal())));
                    tr.setRotate_A(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.rotate_A.ordinal())));
                    tr.setRotate_B(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.rotate_B.ordinal())));
                    tr.setChipType(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.chipType.ordinal())));
                    tr.setSendPower(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.sendPower.ordinal())));
                    tr.setHardwareVer(cursor.getString(HH_EnumBindDeviceINI.hardwareVer.ordinal()));
                    tr.setDeviceName(cursor.getString(HH_EnumBindDeviceINI.deviceName.ordinal()));
                    tr.setContraryROW(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.contraryROW.ordinal())));
                    tr.setContraryCOL(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.contraryCOL.ordinal())));
                    tr.setKeyCount(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.keyCount.ordinal())));
                    tr.setDeviceNumber(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.deviceNumber.ordinal())));
                    tr.setDeviceType(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.deviceType.ordinal())));

                    personList.add(tr);
                }
                db.close();
                return personList;
            }

            db.close();
        }

        return personList;
    }


    public HH_T_BindDevice queryItem(String mac) {
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        if (db.isOpen()) {

            String[] columns = new String[HH_EnumBindDeviceINI.values().length];
            for (HH_EnumBindDeviceINI e : HH_EnumBindDeviceINI.values()) {
                columns[e.ordinal()] = e.name();
            }

            String selection = HH_EnumBindDeviceINI.macRes.toString() + " = ?"; // 选择条件，给null查询所有
            String[] selectionArgs = {mac};//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null; //排序

            Cursor cursor = db.query(HH_SheetName.SQL_SHEET_NAME_BIND_DEVICE, columns, selection, selectionArgs, groupBy, having, orderBy);

            if (cursor != null && cursor.moveToFirst()) {  //cursor不位空,可以移动到第一行

                HH_T_BindDevice tr = new HH_T_BindDevice();
                tr.setProType(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.proType.ordinal())));
                tr.setName(cursor.getString(HH_EnumBindDeviceINI.name.ordinal()));
                tr.setMacRes(cursor.getString(HH_EnumBindDeviceINI.macRes.ordinal()));
                tr.setConnectState(HH_CharUtil.SToB(cursor.getString(HH_EnumBindDeviceINI.connectState.ordinal())));
                tr.setDbm(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.dbm.ordinal())));
                tr.setSelect(HH_CharUtil.SToB(cursor.getString(HH_EnumBindDeviceINI.sel.ordinal())));
                tr.setPicPath_A(cursor.getString(HH_EnumBindDeviceINI.picPath_A.ordinal()));
                tr.setPicPath_B(cursor.getString(HH_EnumBindDeviceINI.picPath_B.ordinal()));
                tr.setBwReversal(HH_CharUtil.SToB(cursor.getString(HH_EnumBindDeviceINI.bwReversal.ordinal())));
                tr.setBz(cursor.getString(HH_EnumBindDeviceINI.bz.ordinal()));
                tr.setDeviceColorType(HH_EnumDeviceColorType.values()[HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.deviceColorType.ordinal()))]);
                tr.setPicIndex(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.picIndex.ordinal())));
                tr.setPicAttr(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.picAttr.ordinal())));
                tr.setVer(cursor.getString(HH_EnumBindDeviceINI.ver.ordinal()));
                tr.setWidth(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.width.ordinal())));
                tr.setHeight(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.height.ordinal())));
                tr.setSize(cursor.getString(HH_EnumBindDeviceINI.size.ordinal()));
                tr.setBatteryB(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.batteryB.ordinal())));
                tr.setOnlineState(HH_EnumOnLineState.values()[HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.onlineState.ordinal()))]);
                tr.setIsWithNewspush(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.isWithNewspush.ordinal())));
                tr.setIsWithFrontLight(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.isWithFrontLight.ordinal())));
                tr.setIsWithLED(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.isWithLED.ordinal())));
                tr.setRotate_A(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.rotate_A.ordinal())));
                tr.setRotate_B(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.rotate_B.ordinal())));
                tr.setChipType(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.chipType.ordinal())));
                tr.setSendPower(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.sendPower.ordinal())));
                tr.setHardwareVer(cursor.getString(HH_EnumBindDeviceINI.hardwareVer.ordinal()));
                tr.setDeviceName(cursor.getString(HH_EnumBindDeviceINI.deviceName.ordinal()));
                tr.setContraryROW(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.contraryROW.ordinal())));
                tr.setContraryCOL(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.contraryCOL.ordinal())));
                tr.setKeyCount(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.keyCount.ordinal())));
                tr.setDeviceNumber(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.deviceNumber.ordinal())));
                tr.setDeviceType(HH_CharUtil.Atoi(cursor.getString(HH_EnumBindDeviceINI.deviceType.ordinal())));


                db.close();
                return tr;
            }

            db.close();
        }

        return null;
    }

}